//
//  SelectMenuViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/30.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "NormalTitleView.h"

#import "SelectMenuCell.h"
#import "MenuSegmentViewController.h"
#import "V2SelectDishesViewController.h"
#import "SubmitFieldOrderViewController.h"
#import "SelectMenuViewController.h"
@interface SelectMenuViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;

@property (nonatomic, strong) UIButton *handleBtn;

@end

@implementation SelectMenuViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)initView {
    
    self.title = @"选择菜单";
    
    [self.view addSubview:self.tableview];
    
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(14);
        make.right.mas_equalTo(-14);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(-60-Bottom_SafeHeight);
    }];
    
    [self.view addSubview:self.handleBtn];
    [self.handleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(14);
        make.right.mas_equalTo(-14);
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(self.tableview.mas_bottom).offset(10);
    }];
    
    [self.tableview reloadData];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.handleBtn pp_clipCorners:UIRectCornerAllCorners radius:self.handleBtn.frame.size.height/2];
    });
}

- (void)handleAction {
    
    NSInteger selCount = 0;
    NSInteger selIndex = 0;
    for (CommonMenuModel *model in self.menuArray) {
        if (model.isSelect) {
            selCount++;
            selIndex = [self.menuArray indexOfObject:model];
        }
    }
    
    if (selCount > 0) {
        
        //如果没有组合菜品直接跳提交订单
        NSInteger combCount = 0;
        for (V2CommonMenuModel *model in self.menuArray[selIndex].dishesList) {
            if (model.dishesList.count > 0) {
                combCount++;
            }
        }
        
        if (combCount > 0) {
            
            V2SelectDishesViewController *vc = [[V2SelectDishesViewController alloc] init];
            vc.menuArray = self.menuArray;
            vc.model = self.model;
            [self.navigationController pushViewController:vc animated:YES];
            
        } else {
            
            SubmitFieldOrderViewController *vc = [[SubmitFieldOrderViewController alloc] init];
            vc.model = self.model;
            vc.menuArray = self.menuArray;
            [self.navigationController pushViewController:vc animated:YES];
        }
        

    } else {
        [SVProgressHUD showErrorWithStatus:@"请选择一个菜单"];
        [SVProgressHUD dismissWithDelay:2];
    }
}

#pragma mark UITableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.menuArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NormalTitleView *headView  =  [[NSBundle mainBundle] loadNibNamed:@"NormalTitleView"owner:nil options:nil].firstObject;
    headView.titleLab.text = @"确定菜单";
    return headView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SelectMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SelectMenuCellID"];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    [cell fillCellWithModel:self.menuArray[indexPath.row]];
    
    WEAKSELF
    cell.SelectMenuCellBlock = ^(SelectMenuCellActionType type) {
        
        switch (type) {
            case SelectMenuCellActionType_sel:{
                
                for (CommonMenuModel *model in weakSelf.menuArray) {
                    model.isSelect = NO;
                }
                weakSelf.menuArray[indexPath.row].isSelect = YES;
            } break;
            case SelectMenuCellActionType_add: {
                CommonMenuModel *model = weakSelf.menuArray[indexPath.row];
                model.bhmMenuCount++;
                weakSelf.menuArray[indexPath.row] = model;
            } break;
            case SelectMenuCellActionType_cut: {
                CommonMenuModel *model = weakSelf.menuArray[indexPath.row];
                if (model.bhmMenuCount >=2) {
                    model.bhmMenuCount--;
                    weakSelf.menuArray[indexPath.row] = model;
                } else {
                }
            } break;
            default:
                break;
        }
        [weakSelf.tableview reloadData];
    };
    
    cell.textFieldShouldEndEditingBlock = ^(UITextField * _Nonnull textField) {
        NSInteger count = [textField.text integerValue];
        weakSelf.menuArray[indexPath.row].bhmMenuCount = count;
    };

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MenuSegmentViewController *vc = [[MenuSegmentViewController alloc] init];
    vc.selectIndex = indexPath.row;
    vc.menuArray = self.menuArray;
    [self.navigationController pushViewController:vc animated:YES];
}

- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor =  UIColorHex(0xf7f7f7);
        
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.showsVerticalScrollIndicator = NO;
        
        _tableview.estimatedSectionHeaderHeight = 0;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.estimatedRowHeight = 0;
        
        [_tableview registerNib:[UINib nibWithNibName:@"SelectMenuCell" bundle:nil] forCellReuseIdentifier:@"SelectMenuCellID"];
    }
    return _tableview;
}

- (UIButton *)handleBtn {
    if (!_handleBtn) {
        _handleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        _handleBtn.backgroundColor = UIColor.redColor;
        [_handleBtn setBackgroundImage:[UIImage imageNamed:@"rect_orange"] forState:UIControlStateNormal];
        [_handleBtn addTarget:self action:@selector(handleAction) forControlEvents:UIControlEventTouchUpInside];
        [_handleBtn setTitle:@"确定菜单" forState:UIControlStateNormal];
        _handleBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    }
    return _handleBtn;
}

@end
